<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>nginx-gray | YouN 科技温度</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="nginx 灰度" />
  
  
  
  
  <meta name="description" content="Nginx 灰度测试 灰度测试在系统重构过程中是必不可少的，很多公司有自己的灰度解决方案。对于初创型或者小型团队，缺少必要的工具，因此这里借用nginx的转发（反向代理）能力做一下灰度测试。  Nginx配置Nginx配置文件主要分4部分 main(全局设置)：  main部分的指令将影响其他所有的设置 server(主机设置)： server部分的指令主要作用于指定的主机和端口 upstream">
<meta property="og:type" content="article">
<meta property="og:title" content="nginx-gray">
<meta property="og:url" content="http://this4u.cn/2019/11/20/nginx-gray/index.html">
<meta property="og:site_name" content="YouN 科技温度">
<meta property="og:description" content="Nginx 灰度测试 灰度测试在系统重构过程中是必不可少的，很多公司有自己的灰度解决方案。对于初创型或者小型团队，缺少必要的工具，因此这里借用nginx的转发（反向代理）能力做一下灰度测试。  Nginx配置Nginx配置文件主要分4部分 main(全局设置)：  main部分的指令将影响其他所有的设置 server(主机设置)： server部分的指令主要作用于指定的主机和端口 upstream">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-11-20T09:52:25.000Z">
<meta property="article:modified_time" content="2023-04-13T05:58:10.715Z">
<meta property="article:author" content="Timmy">
<meta property="article:tag" content="nginx 灰度">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="YouN 科技温度" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">

  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Oswald%3A300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  
<link rel="stylesheet" href="/css/style.css">


  
<script src="/js/jquery-3.1.1.min.js"></script>


  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/fashion.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >

<meta name="generator" content="Hexo 6.3.0"></head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  


<header id="allheader" class="site-header" role="banner" 
   >
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" rel="home" >
                <img style="margin-bottom: 2px;"  width="126px" height="60px" alt="Hike News" src=" /css/images/youu.jpg">
              </a>
            
          </h1>
          
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>

            <div class="clearfix sf-menu">
              <ul id="main-nav" class="menu sf-js-enabled sf-arrows"  style="touch-action: pan-y;">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663" linktext="/"> <a class="" href="/">首页</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663" linktext="archives"> <a class="" href="/archives">归档</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663" linktext="categories"> <a class="" href="/categories">分类</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663" linktext="tags"> <a class="" href="/tags">标签</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663" linktext="about"> <a class="" href="/about">关于</a> </li>
                    
              </ul>
            </div>
          </nav>

      </div>
  </div>
</header>


  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-nginx-gray" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      nginx-gray
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2019/11/20/nginx-gray/" class="article-date">
	  <time datetime="2019-11-20T09:52:25.000Z" itemprop="datePublished">十一月 20, 2019</time>
	</a>

       
      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="Nginx-灰度测试"><a href="#Nginx-灰度测试" class="headerlink" title="Nginx 灰度测试"></a>Nginx 灰度测试</h2><blockquote>
<p>灰度测试在系统重构过程中是必不可少的，很多公司有自己的灰度解决方案。对于初创型或者小型团队，缺少必要的工具，因此这里借用nginx的转发（反向代理）能力做一下灰度测试。</p>
</blockquote>
<h2 id="Nginx配置"><a href="#Nginx配置" class="headerlink" title="Nginx配置"></a>Nginx配置</h2><h3 id="Nginx配置文件主要分4部分"><a href="#Nginx配置文件主要分4部分" class="headerlink" title="Nginx配置文件主要分4部分"></a>Nginx配置文件主要分4部分</h3><ul>
<li><strong>main(全局设置)：</strong>  main部分的指令将影响其他所有的设置</li>
<li><strong>server(主机设置)：</strong> server部分的指令主要作用于指定的主机和端口</li>
<li><strong>upstream(负载均衡服务器设置)：</strong> upstream指令主要作用于负载均衡的设置</li>
<li><strong>location(指定网页的设置):</strong> 主要用于匹配上的网页的设置。首先匹配 &#x3D;，其次匹配^~, 其次是按文件中顺序的正则匹配，最后是交给 &#x2F; 通用匹配。当有匹配成功时候，停止匹配，按当前匹配规则处理请求。</li>
</ul>
<h3 id="proxy-pass"><a href="#proxy-pass" class="headerlink" title="proxy_pass"></a>proxy_pass</h3><blockquote>
<p>在nginx中配置proxy_pass代理转发时，如果在proxy_pass后面的url加&#x2F;，表示绝对根路径；如果没有&#x2F;，表示相对路径，把匹配的路径部分也给代理走。</p>
</blockquote>
<p>假设下面四种情况分别用 <a target="_blank" rel="noopener" href="http://192.168.1.1/proxy/test.html">http://192.168.1.1/proxy/test.html</a> 进行访问。</p>
<p><strong>CASE 1:</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location /proxy/ &#123;</span><br><span class="line">    proxy_pass http://127.0.0.1/;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>代理到URL：<a target="_blank" rel="noopener" href="http://127.0.0.1/test.html">http://127.0.0.1/test.html</a></p>
<p><strong>CASE 2:</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location /proxy/ &#123;</span><br><span class="line">    proxy_pass http://127.0.0.1;#少了一个/</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>代理到URL：<a target="_blank" rel="noopener" href="http://127.0.0.1/proxy/test.html">http://127.0.0.1/proxy/test.html</a></p>
<p><strong>CASE 3:</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location /proxy/ &#123;</span><br><span class="line">    proxy_pass http://127.0.0.1/v1/;#加了一个子路径</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>代理到URL：<a target="_blank" rel="noopener" href="http://127.0.0.1/v1/test.html">http://127.0.0.1/v1/test.html</a></p>
<p><strong>CASE 4:</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location /proxy/ &#123;</span><br><span class="line">    proxy_pass http://127.0.0.1/v1;#加了一个前缀</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>代理到URL：<a target="_blank" rel="noopener" href="http://127.0.0.1/v1test.html">http://127.0.0.1/v1test.html</a></p>
<h2 id="灰度粒度"><a href="#灰度粒度" class="headerlink" title="灰度粒度"></a>灰度粒度</h2><h3 id="URL级别"><a href="#URL级别" class="headerlink" title="URL级别"></a>URL级别</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">location  /api_to_gray &#123;</span><br><span class="line">	proxy_pass http://target_endpoint/api_to_gray;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="参数级别"><a href="#参数级别" class="headerlink" title="参数级别"></a>参数级别</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">location  /api_to_gray &#123;</span><br><span class="line">  //$arg_param 是url里面的参数，$http_param 是header里面的参数</span><br><span class="line">  if ($arg_param = &quot;value&quot;)&#123;</span><br><span class="line">    add_header Version &#x27;V2&#x27;;</span><br><span class="line">    proxy_pass http://target_endpoint/api_to_gray;</span><br><span class="line">  &#125;</span><br><span class="line">	proxy_pass http://original_endpoint;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h2 id="参考文档"><a href="#参考文档" class="headerlink" title="参考文档"></a>参考文档</h2><p><a target="_blank" rel="noopener" href="http://yzone.net/blog/53">Nginx 内置变量</a></p>

      
    </div>
    <footer class="entry-meta entry-footer">
      
      
  <span class="ico-tags"></span>
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/nginx-%E7%81%B0%E5%BA%A6/" rel="tag">nginx 灰度</a></li></ul>

      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTQ4MS82MDQ5">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
    </footer>
    <hr class="entry-footer-hr">
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/04/24/iptables/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          iptables
        
      </div>
    </a>
  
  
    <a href="/2019/11/06/file-diff/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">Linux 文件对比命令</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Nginx-%E7%81%B0%E5%BA%A6%E6%B5%8B%E8%AF%95"><span class="nav-number">1.</span> <span class="nav-text">Nginx 灰度测试</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Nginx%E9%85%8D%E7%BD%AE"><span class="nav-number">2.</span> <span class="nav-text">Nginx配置</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Nginx%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%BB%E8%A6%81%E5%88%864%E9%83%A8%E5%88%86"><span class="nav-number">2.1.</span> <span class="nav-text">Nginx配置文件主要分4部分</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#proxy-pass"><span class="nav-number">2.2.</span> <span class="nav-text">proxy_pass</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%81%B0%E5%BA%A6%E7%B2%92%E5%BA%A6"><span class="nav-number">3.</span> <span class="nav-text">灰度粒度</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#URL%E7%BA%A7%E5%88%AB"><span class="nav-number">3.1.</span> <span class="nav-text">URL级别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%82%E6%95%B0%E7%BA%A7%E5%88%AB"><span class="nav-number">3.2.</span> <span class="nav-text">参数级别</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E6%A1%A3"><span class="nav-number">4.</span> <span class="nav-text">参考文档</span></a></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>

    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
		<a href="https://beian.miit.gov.cn/" target="_blank">鄂ICP备2023005434号-1</a>  	
	        Copyright &copy; 2023 YouN 科技温度 All Rights Reserved.
        
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hipaper" target="_blank">hipaper</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");

    wrapdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";


    <!-- headerblur min height -->
    
    
</script>

    
<div style="display: none;">
  <script src="https://s11.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
</div>

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>


<script src="/js/bootstrap.js"></script>


<script src="/js/main.js"></script>








  <div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
